

$(document).ready(function () {


    if(islogin()){

        sessionStorage.setItem('userChooseLocationCode',getUserInfo().region.code);       //更新来自登录用户的code
        $(".locationCity ").html(getUserInfo().region.name);

    }

    function getTempSessionStorageItem(item){

        let value= sessionStorage.getItem(item);

        sessionStorage.removeItem(item);

        return value;

    }

    let   rank;         //排序
    let    max;         //最高额度
    let    min;         //最低额度
    let   typeName;     //产品类型
    let   Request;      //地址参数
    let    dis;         //地址参数
    let    needLoansCityCode=sessionStorage.getItem('autolocationcode'); //位置默认选择自动定位

    let classProductLimit=$('.productLimit'); //额度
    let classProductType=$('#productTypeUl');   //产品类型
    let classProductRank=$('.productRank');   //产品类型
    let idProductList=$('.productList');      //产品列表盒子

    //
    // rank=getTempSessionStorageItem("anchorRank");
    // max=getTempSessionStorageItem("anchorMax");
    // min=getTempSessionStorageItem("anchorMin");
    // typeName=getTempSessionStorageItem("anchorTypeName");
    //
    //


        Request = new searchUrl(); //地址参数化。方法来自base.js

        dis=decodeURI(Request.dis);





    /****************************获取产品类型*****************************/

        function setProductTypeList(){
            myAjaxFunc({
                url:geturl+"getProperty" ,
                type:'get',
                dataType:'json',
                mySuccess:function (data) {
                    var propertyList=data.data.propertyList,
                        content='';
                    for(var i= 1,lgt=propertyList.length;i<lgt;i++){
                        content+='<li class="productType">'+propertyList[i].name+'</li>'
                    }
                    classProductType.append(content);


                /****************************首页回传参数*****************************/

                if(notNull(dis)){

                    sessionStorage.setItem('anchorTypeName',dis);

                    $('#productTypeUl li').each(function(){
                        var monytext= $(this).text();
                        if(monytext==dis){
                            $('#loansTypeList_ul li').removeClass('Liclick');
                            $(this).addClass('Liclick');
                        }
                    });

                }

                }
            });
        }
    setProductTypeList()


    /****************************选择额度*****************************/


    classProductLimit.click(function () {
        let _this=$(this);

        _this.parent().find('.productLimit').removeClass('Liclick');
        _this.addClass('Liclick');
        sessionStorage.setItem('anchorMax',_this.data('max'));
        sessionStorage.setItem('anchorMin',_this.data('min'));

        getNeedloansproductList(1)
    });


    /****************************选择产品类型*****************************/



    classProductType.on('click','li',function () {


        let _this=$(this);

        _this.parent().find('.productType').removeClass('Liclick');
        _this.addClass('Liclick');

        sessionStorage.setItem('anchorTypeName',_this.text());
        getNeedloansproductList(1)
    });


    /****************************选择产品排序*****************************/


    classProductRank.click(function () {
        let _this=$(this);

        _this.parent().find('.productRank').removeClass('Liclick');
        _this.addClass('Liclick');
        sessionStorage.setItem('anchorRank',_this.data('rank'));

        getNeedloansproductList(1)

    });

        //请求数据
    function getNeedloansproductList(currentPage){

        rank=sessionStorage.getItem("anchorRank");
        max=sessionStorage.getItem("anchorMax");
        min=sessionStorage.getItem("anchorMin");

        if(notNull(dis)){
            typeName=getTempSessionStorageItem('anchorTypeName');
        }else{
            typeName=sessionStorage.getItem("anchorTypeName");
        }


        if(notNull(sessionStorage.getItem('userChooseLocationCode'))){
            needLoansCityCode=sessionStorage.getItem('userChooseLocationCode')
        }


        myAjaxFunc({
            url:geturl+"getPageDate" ,
            type:'get',
            dataType:'json',
            data:{
                regionCode:needLoansCityCode,
                limitHigh:max,
                limitLow:min,
                propertyName:typeName,
                sortName:rank,
                listCurrentPage:currentPage,

            },  mySuccess:function (data) {

                let  content=data.data.pageDate.list
                    ,contentpage=data.data.pageDate.extra
                    ,loansMap='';


                let atpage=contentpage.listCurrentPage,
                    allpage=contentpage.listPageCount;

                creationPage(allpage,atpage)

                //返回的数据加载到页面
                for(var i=0,ig=content.length;i<ig;i++){
                    var ilist=content[i].tagList,
                        icontent='',
                        markshow='';
                    for(var j=0,jg=ilist.length;j<jg;j++){
                        icontent +='<span>'+ilist[j]+'</span>'
                    }


                    let marks = content[i].mark,

                        a = content[i].loanRate,        //利率
                        b = content[i].loanRateUnit,    //利率单位
                        c = content[i].loanLimitLow,    //低额度
                        d = content[i].loanLimitHigh,   //高额度
                        e = content[i].loanSpeed,       //放款速度
                        f = content[i].loanSpeedUnit;   //放款单位

                    if(marks=='hot'){
                        markshow='hotBg'
                    }
                    if (marks=='new'){
                        markshow='newBg'
                    }



                    loansMap+=
                        '      <div class="productItem">' +
                        '            <img  src="'+ geturlimg+content[i].logoUrl+'">' +
                        '            <div class="productInfoText">' +
                        '                <h4 class="productName">'+content[i].name+'</h4>' +
                        '                <div class="text">' +
                        '                    <div class="productInfo">'+icontent+'</div>' +
                        '                    <div class="limitRate">' +
                        '                        <span>'+a+'%/'+b+'</span>' +
                        '                        <span>'+c+'-'+d+'元</span>' +
                        '                    </div>' +
                        '                    <div class="LoanCondition">' +
                        '                        申请条件字段该接口暂无<br>' +
                        '                        等待接口更新放出' +
                        '                    </div>' +
                        '' +
                        '                </div>' +
                        '            </div>' +
                        '            <div class="clickBtnList">' +
                                                                //pcapply判断是否支持PC端申请贷款，目前暂无，需要新增加字段
                        '               <a href="javascript:;"  data-logourl="'+ geturlimg+content[i].logoUrl+'" data-pcapply=false data-url="'+content[i].applyUrl+'" data-productname="'+content[i].name+'" target="_blank"  class="showqr">立即申请</a>' +
                        '               <a href="loansDetail.html?id='+content[i].id+'" target="_blank">查看详情</a>' +
                        '            </div>' +
                        '' +
                        '' +
                        '        </div>'


                    //change by jack


                }


                idProductList.empty().append(loansMap);

            }
        });
    }

     //分页查询
    function creationPage(allpage,atpage) {


        Page({
            num:allpage,			//页码数
            startnum:atpage,		//指定页码
            elem:$('#page2'),		//指定的元素
            callback:function(n){	//回调函数
                getNeedloansproductList(n)

            }
        });
    }
    getNeedloansproductList(1);


    /****************************侧边栏最新贷款*********************************/

    sidebarfn('sidebarProduct','.NewLoansProductList');


    /****************************立即申请*****************************/

    idProductList.on('click','.showqr',function () {

        if(islogin()){
            let _this=$(this);
            let pcpcapply=_this.data('pcapply');
            let applyUrl=_this.data('url');
            let logoUrl=_this.data('logourl');
            let productname=_this.data('productname');

            if(pcpcapply){
                upproductApply(applyUrl)
                // window.location.href=applyUrl;
            }else {
                upproductApply();

                $("#qrProductLogo").attr("src",logoUrl);

                $('#qrCodeBoxInner').html('');

                $('#productname').html(productname);

                new QRCode(document.getElementById('qrCodeBoxInner'), applyUrl);
                $('.qr').show();

                $(".qr").click(function () {
                    $(this).hide();
                });
            }
        }else {
            $('#loginMode').show()
        }




    });



        // creationPage(10,1)


    var eml= document.getElementsByClassName('toNewLoans');


    eml[0].innerHTML='本周新上';
    eml[1].innerHTML='额度高';



});







